package com.mfangsoft.zhuangjialong.integration.activity.controller;





import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.facebook.presto.sql.parser.SqlBaseParser.UseContext;
import com.mfangsoft.zhuangjialong.app.entryactivity.model.BaseEntryActivity;
import com.mfangsoft.zhuangjialong.app.entryactivity.service.EntryActivityService;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.common.utils.UserSessionUtil;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfo;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfoItem;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityShop;
import com.mfangsoft.zhuangjialong.integration.activity.service.ActivityService;
import com.mfangsoft.zhuangjialong.integration.order.model.BaseOrder;
import com.mfangsoft.zhuangjialong.integration.permission.model.PermissionEntity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.model.BaseStatisticsActivity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.service.StatisticsActivityService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;



@Controller
@RequestMapping("/server")
public class ActivityController {
	
	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private UserService userService; 
	
	@Autowired
	private StatisticsActivityService statisticsActivityService; 
	
	 
	

	/**
	 * 管理员活动新增模板接口   联单活动
	 * @param baseActivityInfo
	 * @return
	 */
	
	@RequestMapping(value="/addAdminActivity",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addAdminActivity(@RequestBody BaseActivityInfo baseActivityInfo){
		ResponseMessage<String> message = new ResponseMessage<String>();
	
		try {	
			
			Long aaa = UserContext.getCurrentUser().getId();
			baseActivityInfo.setCreater(aaa.intValue());//创建人
			Long uid = UserContext.getCurrentUser().getId();
			baseActivityInfo.setUser_id(uid.intValue());
		    activityService.addAdminActivity(baseActivityInfo);//创建活动	    
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;	
	}
	
	
	/**
	 * 活动新增接口   联单活动
	 * @param baseActivityInfo
	 * @return
	 */
	
	@RequestMapping(value="/addActivity",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addActivity(@RequestBody BaseActivityInfo baseActivityInfo){
		ResponseMessage<String> message = new ResponseMessage<String>();
		//BaseActivityInfo baseActivityInfo = new BaseActivityInfo();
		//UseContext获取登录信息
		/*System.out.println(UserContext.getCurrentUser());
		System.out.println(UserContext.getCurrentUser().getUserInfoEntity().getName());*/
		try {	
			baseActivityInfo.setAudit_state(1);
			baseActivityInfo.setState(1);
			Long aaa = UserContext.getCurrentUser().getId();
			baseActivityInfo.setCreater(aaa.intValue());//创建人
			Long uid = UserContext.getCurrentUser().getId();
			baseActivityInfo.setUser_id(uid.intValue());
			UserEntity currentUser = UserContext.getCurrentUser();
			baseActivityInfo.setActivity_type(1);//1联单活动
		    String aCode =  createCode(); //生成活动号 
		    baseActivityInfo.setActivity_code(aCode);
		    baseActivityInfo.setPutaway(1);//1上架
		    activityService.addActivbity(baseActivityInfo);//创建活动	    
		    BaseStatisticsActivity statistics =new BaseStatisticsActivity();
		    statistics.setActivity_id(baseActivityInfo.getId());
		    statistics.setUsenum(0);
		    statisticsActivityService.addStatistics(statistics);//创建活动统计表
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;	
	}
	
	public String createCode(){
		String s=""+new Date().getTime();
		String code="HDM"+s.substring(s.length()-3,s.length())+Math.round((Math.random()*9+1) * 1000000);

		Integer activityCode = activityService.selectActivityCode(code);
		if(activityCode>0){
			return createCode();
		}else{
			return code;
		}
	}
	
	/**
	 * 活动列表    分公司
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
//	@RequestMapping(value="/selectActivityPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin 
	public Page<BaseActivityInfo> selectActivityPage(@RequestBody Page<BaseActivityInfo> page) throws ParseException{
		
		return activityService.selectActivity(page);
	}
	
	/**
	 * 管理员活动模板列表      创建活动按钮
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectAdminActivityPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin 
	public Page<BaseActivityInfo> selectAdminActivityPage(@RequestBody Page<BaseActivityInfo> page) throws ParseException{
		
		return activityService.selectAdminActivityPage(page);
	}
	/**
	 * 活动审核列表
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectActivityAuditPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin 
	public Page<BaseActivityInfo> selectActivityAuditPage(@RequestBody Page<BaseActivityInfo> page) throws ParseException{
		
		
		return activityService.selectActivityAuditPage(page);
	}
	/**
	 * 活动批量修改
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/updateActivity",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String> updateActivity(@RequestBody List<BaseActivityInfo> list) throws ParseException{
		
		ResponseMessage<String> message = new ResponseMessage<String>();
	
		try {
			
			for (BaseActivityInfo baseActivityInfo:list) {
				/*BaseActivityInfo baseActivityInfo  = new BaseActivityInfo();*/
			
				activityService.updateActivity(baseActivityInfo);//修改活动状态
			}	
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 活动审核状态修改
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value="/updateActivityStateOne",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<String> updateActivityStateOne(@RequestBody BaseActivityInfo baseActivityInfo,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<String> message = new ResponseMessage<String>();
	
		try {
			activityService.updateActivity(baseActivityInfo);
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 活动上下架状态
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/updateActivityPutaway",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateActivityPutaway(@RequestBody BaseActivityInfo baseActivityInfo,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<String> message = new ResponseMessage<String>();
		/*BaseActivityInfo baseActivityInfo = new BaseActivityInfo();*/

		try {
		activityService.updateActivityPutaway(baseActivityInfo);//修改上下架状态
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 活动查询
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectActivityOne/{id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectActivityOne(@PathVariable Long id,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectActivityOne(id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 活动编辑
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/updateActivityOne",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<BaseActivityInfo> updateActivityOne(@RequestBody BaseActivityInfo baseActivityInfo,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<BaseActivityInfo> message = new ResponseMessage<BaseActivityInfo>();
		/*BaseActivityInfo baseActivityInfo = new BaseActivityInfo();
		baseActivityInfo.setId(25);*/
		try {
			activityService.updateActivityOne(baseActivityInfo);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	/**
	 * 活动内容
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectActivityItem",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<BaseActivityInfo>> selectActivityItem(HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<List<BaseActivityInfo>> message = new ResponseMessage<List<BaseActivityInfo>>();
		BaseActivityInfo baseActivityInfo = new BaseActivityInfo();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectActivityItem(baseActivityInfo));
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	/**
	 * 获取所有店铺
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectUserShop",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<Map<String,Object>>> selectUserShop(@RequestBody BaseOrder baseOrder,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<List<Map<String,Object>>> message = new ResponseMessage<List<Map<String,Object>>>();
		
		try {
			/*System.out.println(UserContext.getCurrentUser());
			System.out.println(UserContext.getCurrentUser().getUserInfoEntity().getName());*/
			Map<String,Object> map= new HashMap<>();
			
			map.put("province", baseOrder.getProvince());
			map.put("city", baseOrder.getCity());
			map.put("district", baseOrder.getDistrict());
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			if(UserContext.getCurrentUser().getUser_level() ==2){
			message.setResult(userService.getUserByLevel(2, UserContext.getCurrentUser().getId(),map,1));
			}else {
			message.setResult(userService.getUserByLevel(UserContext.getCurrentUser().getUser_level(), UserContext.getCurrentUser().getId(),map,1));
			}
			} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 活动参与人 个人详情
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectActivityUser",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectActivityUser(@RequestBody BaseEntryActivity baseEntryActivity) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			/*BaseEntryActivity baseEntryActivity = new BaseEntryActivity();
			baseEntryActivity.setActivity_id(58);
			baseEntryActivity.setCustomer_id(10);*/
			message.setResult(activityService.selectActivityUser(baseEntryActivity));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	/**
	 * 活动管理  活动查看列表，显示所有报名人和详情页面
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectActivityUsers",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage <Page<Map<String, Object>>>  selectActivityUsers(@RequestBody Page<Map<String, Object>> page) throws ParseException{
		
		ResponseMessage<Page<Map<String, Object>>> message = new ResponseMessage<Page<Map<String, Object>>>();
		/*BaseActivityInfo baseActivityInfo = new BaseActivityInfo();*/
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectActivityUsers(page));
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 活动发布列表查询
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectActivityIssueList/{activity_id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<Map <String,Object>>> selectActivityIssueList(@PathVariable Integer activity_id,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<List<Map <String,Object>>> message = new ResponseMessage<List<Map <String,Object>>>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectActivityIssueList(activity_id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

}
